package gui;

/***
 * @author Sebastian Mæland Pedersen
 */

import guilib.LibraryTreeModel;

import java.util.Observable;

import javax.swing.JTree;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.TreeSelectionModel;

public class Tree extends Observable {
	private JTree tree;
	private LibraryTreeModel model;

	public Tree() {
		model = new LibraryTreeModel();
		
		tree = new JTree(model);
		
		tree.getSelectionModel().setSelectionMode(
				TreeSelectionModel.SINGLE_TREE_SELECTION);

		tree.setRootVisible(false);
		tree.setShowsRootHandles(true);
		tree.addTreeSelectionListener(new TreeSelectionListener() {
		
			@Override
			public void valueChanged(TreeSelectionEvent arg0) {
				if (getSelected() == null) return;

				setChanged();
				notifyObservers();
			}
		});
	}
	
	public JTree getTree() {
		return tree;
	}
	
	public LibraryTreeModel getModel() {
		return model;
	}
	
	public Object getSelected() {
		return tree.getLastSelectedPathComponent();
	}
}
